<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closeDialog" @open="openbtn">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="hAssignRoles">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: [],
      roleIds: []
    }
  },
  methods: {
    // 关闭弹层
    closeDialog() {
      this.$emit('showRoleDialog')
    },
    async getRoleList() {
      const res = await getRoleList(1, 100)
      this.list = res.data.rows
    },
    // 弹框打开获取角色
    async openbtn() {
      this.getRoleList()
      this.hGetUserDetailById()
    },
    // 点击确定
    async hAssignRoles() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.closeDialog()
      this.$message.success('分配角色成功')
    },
    async hGetUserDetailById() {
      const res = await getUserDetailById(this.userId)
      this.roleIds = res.data.roleIds
    }
  }
}
</script>
